<template>
    <div>
        <a-form autocomplete="off"
                :model="formState"
                @finish="onFinish"
                name="basic">
            <a-form-item name="password" :rules="[{required: true,message: '请输入新密码'}]">
                <a-input type="password" v-model:value="formState.password" placeholder="请输入新密码"/>
            </a-form-item>
            <a-form-item name="confirmPassword" :rules="[{required: true,message: '请输入确认密码'}]">
                <a-input type="password" v-model:value="formState.confirmPassword" placeholder="确认密码"/>
            </a-form-item>
            <a-form-item>
                <a-button style="width: 100%" size="large" html-type="submit" type="primary">提交
                </a-button>
            </a-form-item>
        </a-form>
    </div>
</template>

<script>
    import {message} from 'ant-design-vue';
    import {request} from "@/utils/request";
    import api from "@/constants/api";

    export default {
        name: "RestPassword",
        data() {
            return {
                formState: {
                    password: '',
                    confirmPassword: '',
                }
            }
        },
        mounted() {
        },

        methods: {
            onFinish(values) {
                const password = values.password;
                const confirmPassword = values.confirmPassword;
                if (password !== confirmPassword) {
                    message.warn("修改密码与确认密码不一致");
                    return;
                }
                const data = new FormData();
                data.append("username", localStorage.getItem("username"))
                data.append("password", values.confirmPassword);
                request({
                    url: api.ADMIN_REST_PASSWORD,
                    data: data,
                    method: 'POST',
                    pureData: false,
                }).then(res => {
                    if (res.code === 200) {
                        message.success("更新成功")
                    } else {
                        message.error("更新失败")
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>